package com.qpx.eureka.producer.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qpx.dto.UserDto;
import com.qpx.eureka.producer.entity.User;
import com.qpx.eureka.producer.service.IUserService;
import com.qpx.vo.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2021-02-21
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    IUserService iUserService;

    @Value("${foo}")
    private String value;

    @RequestMapping("/list")
    public ResultEntity getUserList(@RequestBody UserDto userDto){
        log.info("--------------------------{}",value);
        PageHelper.startPage(userDto.getPageNo(),userDto.getPageSize());
        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper.like("name",userDto.getName());
        List<User> users = iUserService.selectList(wrapper);
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        return ResultEntity.ok("200","查询成功",userPageInfo);
    }

    @RequestMapping("/save")
    public ResultEntity save(@RequestBody UserDto userDto){
        User user = new User();
        BeanUtils.copyProperties(userDto,user);

        iUserService.insert(user);
        return ResultEntity.ok();
    }

}
